#include"personList.h"  

using namespace std;

Person::Person(int identity, string name, string sex)
{
    m_identity = identity;
    m_name = name;
    m_sex = sex;
}

Employee::Employee(int identity, string name, string sex, string dept):Person(identity, name, sex), m_dept(dept){}
void Employee::Display()
{
    cout << " 证件号:" << m_identity << " 姓名:" << m_name << " 性别:" << m_sex << " 部门:" << m_dept << endl;
}

Teacher::Teacher(int identity, string name, string sex, string subject):Person(identity, name, sex), m_subject(subject){}
void Teacher::Display()
{
    cout << " 证件号:" << m_identity << " 姓名:" << m_name << " 性别:" << m_sex << " 科目:" << m_subject << endl;
}

Student::Student(int identity, string name, string sex, int grade):Person(identity, name, sex), m_grade(grade){}
void Student::Display()
{
    cout << " 证件号:" << m_identity << " 姓名:" << m_name << " 性别:" << m_sex << " 年级:" << m_grade << endl;
}

// 模板实现
Node::Node(Person *data):m_data(data), m_next(NULL){}

PersonList::PersonList():m_head(NULL), m_tail(NULL){}
void PersonList::Insert(Person *newNode)
{
    Node *tmp = new Node(newNode);
    if (!m_head) {
        m_head = m_tail = tmp;
        m_length++;
    }
    else {
        m_tail->m_next = tmp;
        m_tail = tmp;
        m_length++;
    }
}
void PersonList::Delete(int identity)
{
    if (!m_head) {
        cout << "链表为空" << endl;
        return;
    }
    Node *pre = m_head;
    if (pre->m_data->m_identity == identity) {
        m_head = pre->m_next;
        delete pre;
        return;
    }
    Node *current;
    for (current = m_head->m_next ; current; current = current->m_next) {
        if (current->m_data->m_identity == identity) {
            pre->m_next = current->m_next;
            delete current;
            return;
        }
        pre = current;
    }
    cout << "没找到" << endl;
}
void PersonList::Print()
{
    if (!m_head) {
        cout << "链表为空" << endl;
        return;
    }
    Node *tmp;
    for (tmp = m_head; tmp; tmp = tmp->m_next) {
        tmp->m_data->Display();
    }
}

void PersonList::Find(int identity)
{
    if (!m_head) {
        cout << "链表为空" << endl;
        return;
    }
    Node *tmp;
    for (tmp = m_head; tmp; tmp = tmp->m_next) {
        if (tmp->m_data->m_identity == identity) {
          tmp->m_data->Display();
          return;
        }
    }
    cout << "没有找到identity=" << identity << endl;
}
